package controller;

import bean.Account;
import dao.AccountDao;
import dao.impl.AccountDaoImpl;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import util.AlertUtil;
import util.FxmlUtil;

import java.io.IOException;
import java.util.Objects;

public class LoginController {
    public TextField accPass;
    public TextField accNo;
    private AccountDao accountDao = new AccountDaoImpl();

    public void login() throws IOException {
        String accNoText = accNo.getText();
        String accPassText = accPass.getText();
        Account account = accountDao.selectByIdAndPass(accNoText, accPassText);
        if (account != null){
            FxmlUtil.DATA.put("account", account);

            AnchorPane pane = FXMLLoader.load(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("fxml/main.fxml")));
            Scene scene = new Scene(pane);
            FxmlUtil.SCENE.put("main", scene);

            Stage stage = (Stage) this.accNo.getScene().getWindow();
            stage.setTitle("ATM - 首页");
            stage.setScene(scene);
        }else {
            AlertUtil.alert(Alert.AlertType.CONFIRMATION, "登录失败","账号或密码错误", ButtonType.OK);
        }
    }

    public void logout() {
        this.accNo.getScene().getWindow().hide();
    }
}
